/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = t_sqlserver_get_datetimes.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/14 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <frcomm.h>
#include <sqlserver/config.h>
#include <sqlserver/sqlserver_core.h>
#include <sqlserver/sqlserver_common.h>

extern int sqlserver_get_datetimes(Frbyte_Packet *p_st_data, LITTLE_BIG_ENDIAN i_endian, int i_whole_flg,
                                   uint8_t u8_data_type, int ui_scale, Frdata_field **pp_st_value);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 1;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    return;
}

static void __case2(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 1;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x06\x01\x02\x03\x04\x05\x06\x08", 8);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2033-10-26T05:28:32.1Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case3(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 2;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x06\x01\x02\x03\x04\x05\x06\x08", 8);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2033-10-26T00:32:51.21Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case4(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 3;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x07\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2077-11-07T18:41:45.985Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case5(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 4;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x07\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2077-11-07T01:52:10.5985Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case6(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 5;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x01\x02\x03\x04\x05\x06\x07\x08\x08", 10);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T59:50:21.42465Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case7(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 6;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x01\x02\x03\x04\x05\x06\x07\x08\x08", 10);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T05:59:02.142465Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case8(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 7;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x01\x02\x03\x04\x05\x06\x07\x08\x08", 10);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T00:35:54.2142465Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case9(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 1;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x01\x02\x03\x04\x05\x06\xE0\x01\xFF", 10);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2033-10-26T05:28:32.1+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case10(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 2;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x01\x02\x03\x04\x05\x06\xE0\x01\x08", 10);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2033-10-26T00:32:51.21+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case11(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 3;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x09\x01\x02\x03\x04\x05\x06\x07\xE0\x01\x08", 11);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2077-11-07T18:41:45.985+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case12(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 4;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x09\x01\x02\x03\x04\x05\x06\x07\xE0\x01\x08", 11);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2077-11-07T01:52:10.5985+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case13(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 5;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x0A\x01\x02\x03\x04\x05\x06\x07\x08\xE0\x01\x08", 12);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T59:50:21.42465+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case14(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 6;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x0A\x01\x02\x03\x04\x05\x06\x07\x08\xE0\x01\x08", 12);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T05:59:02.142465+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case15(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIMEOFFSETN;
    int ui_scale = 7;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x0A\x01\x02\x03\x04\x05\x06\x07\x08\xE0\x01\x08", 12);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "1985-10-13T00:35:54.2142465+08:00");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case16(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_DATETIME2N;
    int ui_scale = 7;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x08\x80\x5A\x09\xD7\x53\x4D\x32\x0B", 9);

    i_ret = sqlserver_get_datetimes(p_st_data, BYTE_LITTLE_ENDIAN, i_whole_flg, u8_data_type, ui_scale, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "2010-01-01T10:00:09.0000000Z");

    bytebuff_free(&p_st_data);
    frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

int add_suite_sqlserver_get_datetimes(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sqlserver_get_datetimes()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL ||
        CU_add_test(pSuite, "case9", __case9) == NULL ||
        CU_add_test(pSuite, "case10", __case10) == NULL ||
        CU_add_test(pSuite, "case11", __case11) == NULL ||
        CU_add_test(pSuite, "case12", __case12) == NULL ||
        CU_add_test(pSuite, "case13", __case13) == NULL ||
        CU_add_test(pSuite, "case14", __case14) == NULL ||
        CU_add_test(pSuite, "case15", __case15) == NULL ||
        CU_add_test(pSuite, "case16", __case16) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
